package org.display.graphic
{
	import flash.display.Sprite;
	import flash.events.*;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.ui.*;
	
	import mx.controls.*;
	import mx.flash.UIMovieClip;
	import mx.managers.*;
	
	import org.data.*;
	import org.utils.*;
		
	public class ShapeArea extends UIMovieClip
	{
		private var maskbg:Sprite
		
		private var container:Sprite
		
		public function ShapeArea(container)
		{
			//createMenuItems(["发布对象类","发布模块类","左端对齐","顶端对齐","竖向居中对齐","横向居中对齐","删除"])
			
			createMenuItems(["发布对象类","发布测试类","左端对齐","顶端对齐","删除"])
			
			this.container=container
			maskbg=new Sprite()
			redraw(0,0)
			
			this.addEventListener(MouseEvent.MOUSE_DOWN,mouseEvent)
			
		}
		
		function mouseEvent(e:MouseEvent){
		
			switch(e.type){
				
				case MouseEvent.MOUSE_DOWN:

					this.startDrag(false,new Rectangle(-parent.x,-parent.y,Global.sceneSize,Global.sceneSize))
					stage.addEventListener(MouseEvent.MOUSE_UP,mouseEvent)
					break
				
				case MouseEvent.MOUSE_UP:
					stopDrag()
					stage.removeEventListener(MouseEvent.MOUSE_UP,mouseEvent)
					break

			}
			
		}
		
		
		public function createMenuItems(items:Array){
			
			var myContextMenu:ContextMenu = new ContextMenu();
			myContextMenu.hideBuiltInItems();
			
			for(var i=0;i<items.length;i++){
				
				createItem(items[i])
				
			}
			
			this.contextMenu = myContextMenu;
			
			function createItem(vlaue:String){
				
				var item:ContextMenuItem = new ContextMenuItem(vlaue);
				item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT ,menuselectEvent)
				myContextMenu.customItems.push(item);
				
			}
			
		}
		
		function menuselectEvent(e:ContextMenuEvent){
			
			var objs:Array=ElementControl.getObjs(ShapeBase,this)

			var rect:Rectangle=getBounds(parent)	
				
			var objrect:Rectangle
				
			ElementControl.cleanObj(maskbg)
			
			switch(e.currentTarget.caption){
				
				case "左端对齐":

					for(var i=0;i<objs.length;i++){
						 var obj=objs[i]
						 obj.x=0
					}
					
					break
				case "顶端对齐":

					for( i=0;i<objs.length;i++){
						obj=objs[i]
						obj.y=0
					}
					
					break
				
				case "横向居中对齐":
					
					for( i=0;i<objs.length;i++){
						obj=objs[i]
						objrect=getBounds(obj)
						obj.y=(rect.height-objrect.height)/2

					}
					
					break
				
				case "竖向居中对齐":
					
					for( i=0;i<objs.length;i++){
						obj=objs[i]
						objrect=getBounds(obj)
						obj.x=(rect.width-objrect.width)/2
					}

					break
				
				case "发布对象类":
				case "发布文档类":
				case "发布测试类":
					
					Global.scene=this
					
					if(Global.lockPath==""){
						Alert.show("请锁定[org/display]或是此路径下任一文件夹作为发布文件夹!")
						return;
					}
					
					var addFolderPannel:ETitlePannel=ETitlePannel(PopUpManager.createPopUp(this.parent, ETitlePannel , true));
					addFolderPannel.title=e.currentTarget.caption
					
					break
				
				
				case "删除":
					
					ElementControl.cleanObj(this)
					
					break
				
				
				
			}
			
			
		    rect=getBounds(this)	
			drawMaskbg(rect.width,rect.height)
			maskbg.x=0
			maskbg.y=0
		
		}
		
		public function mousedownEvent(){
					
				var arr:Array=ElementControl.getObjs(ShapeBase,this)	
				
				for(var i=0;i<arr.length;i++){
					
					var obj=arr[i]
					
					var point:Point=new Point(obj.x,obj.y)
					point=localToGlobal(point)
					point=container.globalToLocal(point)
		
					ElementControl.cleanObj(obj)
					container.addChild(obj)
						
					obj.x=point.x
					obj.y=point.y	
				}	
				
				ElementControl.cleanAll(this)

		}
		
		public function mouseupEvent(){
		
			//第一步清空多余对象
			ElementControl.cleanObj(maskbg)
			//第二步获取所有同级对象	
			var objs:Array=ElementControl.getObjs(ShapeBase,container)
			//第三步将所有包含对象载入	
			var rec:Rectangle=getBounds(container)
			for(var i=0;i<objs.length;i++){
				
				var obj:ShapeBase=objs[i] as ShapeBase
					
				var point:Point=container.localToGlobal(new Point(obj.x,obj.y))
				point=this.globalToLocal(point)
					
				if(obj.x>rec.x&&obj.x<rec.x+rec.width&&obj.y>rec.y&&obj.y<rec.y+rec.height){
				   
					ElementControl.cleanObj(obj)
					addChild(obj)
					
					obj.x=point.x
					obj.y=point.y
						
				   
				}
			}
			
			//第四步清空绘制区域对象
			graphics.clear()
			
			//第五步如果有对象载入
				
			objs=ElementControl.getObjs(ShapeBase,this)
			
			if(objs.length>0){
				
			rec=this.getBounds(container)
				
			for(i=0;i<objs.length;i++){
				
				obj=objs[i] as ShapeBase
				obj.x-=rec.x-x
				obj.y-=rec.y-y	
				
			}
			
			draw(maskbg,rec.width,rec.height)
			addChild(maskbg)		
			maskbg.x=0
			maskbg.y=0
				
			
			x=rec.x
			y=rec.y
				
				
			}else{
				
				ElementControl.cleanObj(maskbg)
				ElementControl.cleanObj(this)
					
			}
			
		}
		
		public function drawMaskbg(w:Number,h:Number){

			draw(maskbg,w,h)
			addChild(maskbg)	
		}
		
		
		public function redraw(w:Number,h:Number){

			draw(this,w,h)
			
		}
		
		function draw(obj,w:Number,h:Number){
			
			with(obj){
				graphics.clear()
				graphics.lineStyle(1,0x000000,1)
				graphics.beginFill(0xcccccc,0.5)
				graphics.drawRect(0,0,w,h)
				graphics.endFill()
			}
			
		}
	}
}